﻿using System.Configuration;
using System.Data;
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using MyLogging;
using MyServices;

namespace EntryProject;

public partial class App : Application
{
    public new static App Current => (App)Application.Current;
    public IServiceProvider Services { get; }
    public App()
    {
        Services = ConfigureServices();
    }
    private static IServiceProvider ConfigureServices()
    {
        var services = new ServiceCollection();
        //添加自己定义的日志服务
        // services.AddSingleton<IMyLogger, MyLogger>();
        // 改造一下把注册放到其他项目中
        //相当于把services借给定义服务的项目来注册
        services.AddMyLogging();
        services.AddMyservices();
        services.AddTransient<MainViewModel>();
        services.AddTransient<MainWindow>(sp => new MainWindow { DataContext = sp.GetService<MainViewModel>() });
        return services.BuildServiceProvider();
    }
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        MainWindow = Services.GetRequiredService<MainWindow>();
        //!不会在运行时检查 null，只是抑制编译器的 null 警告
        MainWindow!.Show();
    }
}